import re
import json
import scrapy

from spiders import *

__all__ = ['SeedItem']


# 种子信息
class SeedItem(scrapy.Item):
    item_type = 'seed'
    job = scrapy.Field(serializer=json.dumps)
    type = scrapy.Field()
    currentPage = scrapy.Field()
    result = scrapy.Field(serializer=json.dumps)

    def __init__(self):
        scrapy.Item.__init__(self)
        self['type'] = 'seed'

    @staticmethod
    def seed_basic_info(seed_category, seed_val, seed_desc=None, seed_publish_date=-1, offsetTime=0, isAjax=1):
        return {
            'seedMajorCategory': 'seed_non_keyword',
            'seedMinorCategory': seed_category,
            'seedVal': str(seed_val),
            'seedDesc': seed_desc,
            'seedPublishDate': seed_publish_date,
            'offsetTime': offsetTime,
            'isAjax': isAjax
        }

    @classmethod
    def url2seeds(cls, url):
        matched_seed = []
        for url_pattern in need_redirect_list:
            if re.match(url_pattern, url):
                url = cls.get_redirect_url(url)

        for seed_map in seed_map_list:
            matchs = re.findall(seed_map['pattern'], url)
            if matchs:
                seed = matchs[0]
                matched_seed.append({
                    **seed_map,
                    'seed_val': seed
                })
        return matched_seed